﻿package frEngine.animateControler
{
    import __AS3__.vec.*;
    import baseEngine.core.*;
    import baseEngine.system.*;
    import flash.display3D.*;
    import flash.events.*;
    import frEngine.*;
    import frEngine.animateControler.keyframe.*;
    import frEngine.animateControler.line.*;
    import frEngine.core.*;
    import frEngine.event.*;
    import frEngine.shader.*;
    import frEngine.shader.filters.*;
    import frEngine.shader.filters.fragmentFilters.*;
    import frEngine.shader.filters.vertexFilters.*;
    import frEngine.shader.registType.*;

    public class DrawShapeControler extends MeshAnimateBase implements IRender
    {
        private var _lineParams:VcParam;
        private var _color:VcParam;
        private var _material:ShaderBase;
        private var _colorFilter:ColorFilter;
        private var _surfaceList:Array;
        public static var lineSizeSclae:Number = 1;

        public function DrawShapeControler()
        {
            this._surfaceList = new Array();
            this._colorFilter = new ColorFilter(0, 0, 0, 0);
            _isPlaying = true;
            return;
        }// end function

        public function clear() : void
        {
            var _loc_1:FrSurface3D = null;
            var _loc_2:* = targetMesh.getSurfacesLen();
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2)
            {
                
                _loc_1 = targetMesh.getSurface(_loc_3);
                _loc_1.disposeImp();
                _loc_3++;
            }
            targetMesh.clearSurface();
            this._surfaceList = new Array();
            return;
        }// end function

        override public function get type() : int
        {
            return AnimateControlerType.DrawShapeControler;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            this.clear();
            super.dispose();
            if (this._material)
            {
                FrEventDispatcher.instance.proxyRemoveEventListener(this._material, Engine3dEventName.MATERIAL_REBUILDER, this.setingParams);
                this._material = null;
            }
            return;
        }// end function

        private function setingParams(event:Event) : void
        {
            FrEventDispatcher.instance.proxyRemoveEventListener(this._material, Engine3dEventName.MATERIAL_REBUILDER, this.setingParams);
            this._lineParams = this._material.getParam("{lineParams}", true) as VcParam;
            return;
        }// end function

        override public function set targetObject3d(param1:Pivot3D) : void
        {
            super.targetObject3d = param1;
            if (this._material)
            {
                FrEventDispatcher.instance.proxyRemoveEventListener(this._material, Engine3dEventName.MATERIAL_REBUILDER, this.setingParams);
                this._material = null;
            }
            if (targetMesh)
            {
                targetMesh.material.materialParams.sourceFactor = Context3DBlendFactor.SOURCE_ALPHA;
                targetMesh.material.materialParams.destFactor = Context3DBlendFactor.ONE_MINUS_SOURCE_ALPHA;
                targetMesh.material.materialParams.depthWrite = true;
                targetMesh.material.materialParams.twoSided = true;
                this._material = targetMesh.material;
                FrEventDispatcher.instance.proxyAddEventListener(this._material, Engine3dEventName.MATERIAL_REBUILDER, this.setingParams);
                targetMesh.setShaderBase(new Line3dFilter(), this._colorFilter);
            }
            return;
        }// end function

        override public function toUpdateAnimate(param1:Boolean = false) : void
        {
            super.toUpdateAnimate(param1);
            if (this._lineParams)
            {
                this._lineParams.value[0] = Global3D.camera.zoom / Device3D.viewPortW * lineSizeSclae;
            }
            return;
        }// end function

        public function lineStyle(param1:Number = 1, param2:uint = 16777215, param3:Number = 1, param4:uint = 0) : void
        {
            var _loc_5:* = this._surfaceList[param4];
            if (!_loc_5)
            {
                var _loc_6:* = new LineSurfaceVo();
                _loc_5 = new LineSurfaceVo();
                this._surfaceList[param4] = _loc_6;
            }
            _loc_5.alpha = param3;
            _loc_5.thickness = param1;
            _loc_5.r = (param2 >> 16 & 255) / 255;
            _loc_5.g = (param2 >> 8 & 255) / 255;
            _loc_5.b = (param2 & 255) / 255;
            return;
        }// end function

        public function moveTo(param1:Number, param2:Number, param3:Number, param4:int = 0) : void
        {
            var _loc_5:* = this._surfaceList[param4];
            _loc_5.lx = param1;
            _loc_5.ly = param2;
            _loc_5.lz = param3;
            return;
        }// end function

        public function drawEdge(param1:Mesh3D, param2:Number = 1, param3:Number = 1, param4:Number = 1) : void
        {
            return;
        }// end function

        public function drawDepth(param1:Mesh3D, param2:Number = 1, param3:Number = 1, param4:Number = 1, param5:Number = 1) : void
        {
            return;
        }// end function

        public function draw(param1:Mesh3D, param2:ShaderBase = null) : void
        {
            var _loc_3:FrSurface3D = null;
            var _loc_6:LineSurfaceVo = null;
            var _loc_7:MaterialParams = null;
            var _loc_4:* = this._surfaceList.length;
            var _loc_5:int = 0;
            while (_loc_5 < _loc_4)
            {
                
                _loc_6 = this._surfaceList[_loc_5];
                _loc_3 = _loc_6.surface;
                if (!this._material.hasPrepared(param1, _loc_3))
                {
                    return;
                }
                _loc_7 = param1.material.materialParams;
                this._colorFilter.colorValue[0] = _loc_6.r;
                this._colorFilter.colorValue[1] = _loc_6.g;
                this._colorFilter.colorValue[2] = _loc_6.b;
                this._colorFilter.colorValue[3] = _loc_6.alpha;
                this._material.draw(param1, _loc_3, _loc_7.depthCompare, _loc_7.cullFace, _loc_7.depthWrite, _loc_7.sourceFactor, _loc_7.destFactor, _loc_3.firstIndex, _loc_3.indexBufferFr.numTriangles);
                var _loc_8:* = Global3D;
                var _loc_9:* = Global3D.objectsDrawn + 1;
                _loc_8.objectsDrawn = _loc_9;
                _loc_5++;
            }
            return;
        }// end function

        public function completeDraw(param1:Mesh3D) : void
        {
            return;
        }// end function

        public function lineTo(param1:Number, param2:Number, param3:Number, param4:int = 0) : void
        {
            var _loc_7:uint = 0;
            var _loc_8:Vector.<Number> = null;
            var _loc_5:* = this._surfaceList[param4];
            var _loc_6:* = _loc_5.surface;
            if (!_loc_6)
            {
                _loc_6 = new FrSurface3D(targetMesh.name + "_surface");
                _loc_6.addVertexData(FilterName_ID.POSITION_ID, 3, false, null);
                _loc_6.addVertexData(FilterName_ID.PARAM0_ID, 3, false, null);
                _loc_6.addVertexData(FilterName_ID.PARAM1_ID, 1, false, null);
                _loc_6.indexVector = new Vector.<uint>;
                targetMesh.addSurface(_loc_6);
                _loc_5.surface = _loc_6;
            }
            _loc_8 = _loc_6.getVertexBufferByNameId(FilterName_ID.POSITION_ID).vertexVector;
            _loc_7 = _loc_8.length / _loc_6.sizePerVertex;
            var _loc_9:* = _loc_5.lx;
            var _loc_10:* = _loc_5.ly;
            var _loc_11:* = _loc_5.lz;
            var _loc_12:* = _loc_5.thickness;
            _loc_8.push(_loc_9, _loc_10, _loc_11, param1, param2, param3, _loc_12, param1, param2, param3, _loc_9, _loc_10, _loc_11, -_loc_12, _loc_9, _loc_10, _loc_11, param1, param2, param3, -_loc_12, param1, param2, param3, _loc_9, _loc_10, _loc_11, _loc_12);
            _loc_6.indexVector.push(_loc_7 + 2, (_loc_7 + 1), _loc_7, (_loc_7 + 1), _loc_7 + 2, _loc_7 + 3);
            _loc_5.lx = param1;
            _loc_5.ly = param2;
            _loc_5.lz = param3;
            return;
        }// end function

    }
}
